package businesslogic.informBl;

import java.rmi.Naming;
import java.util.ArrayList;

import po.informPO.InformPO;
import po.informPO.StudentInformPO;
import po.informPO.TeacherInformPO;
import vo.informVO.InformVO;
import vo.informVO.StudentInformVO;
import vo.informVO.TeacherInformVO;
import businesslogicservice.informblservice.InformBlService;
import dataservice.ResultMessage;
import dataservice.informDataService.InformDataService;

public class Inform implements InformBlService {
	
	private static final String ADDRESS_STRING="localhost";
	
	private String id;
	
	public Inform(String id){
		this.id=id;
	}
	

	@Override
	public ResultMessage sendStudentInform(InformVO informVO) {
		ResultMessage resultMessage=null;
		try {
			InformDataService informDataService=
					(InformDataService)Naming.lookup("rmi://"+ADDRESS_STRING+":8888/informData");
			InformPO informPO=new StudentInformPO(informVO.getId(),informVO.getMessage());
			resultMessage=informDataService.insert(informPO);
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resultMessage;
	}

	@Override
	public ArrayList<StudentInformVO> getStudentInform() {
		InformDataService informDataService;
		ArrayList<StudentInformPO> informPOs=null;
		ArrayList<StudentInformVO> informVOs=new ArrayList<>();
		
		try {
			informDataService=(InformDataService)Naming.lookup("rmi://"+ADDRESS_STRING+":8888/informData");
			informPOs= informDataService.findAllByStudentID(id);
			for(InformPO informPO:informPOs){
				StudentInformVO informVO=new StudentInformVO(informPO.getId(), informPO.getMessage());
				informVOs.add(informVO);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return informVOs;
	}

	@Override
	public ArrayList<TeacherInformVO> getTeacherInform() {
		InformDataService informDataService;
		ArrayList<TeacherInformPO> informPOs=null;
		ArrayList<TeacherInformVO> informVOs=new ArrayList<>();
		
		try {
			informDataService=(InformDataService)Naming.lookup("rmi://"+ADDRESS_STRING+":8888/informData");
			informPOs= informDataService.findAllByTeacherID(id);
			for(InformPO informPO:informPOs){
				TeacherInformVO informVO=new TeacherInformVO(informPO.getId(), informPO.getMessage());
				informVOs.add(informVO);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return informVOs;
	}

	@Override
	public ResultMessage deleteAllInform() {
		InformDataService informDataService;
		ResultMessage resultMessage=null;
		try {
			informDataService=(InformDataService)Naming.lookup("rmi://"+ADDRESS_STRING+":8888/informData");
			resultMessage=informDataService.deleteAll();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resultMessage;
	}


	@Override
	public ResultMessage sendTeacherInform(InformVO informVO) {
		ResultMessage resultMessage=null;
		try {
			InformDataService informDataService=
					(InformDataService)Naming.lookup("rmi://"+ADDRESS_STRING+":8888/informData");
			InformPO informPO=new TeacherInformPO(informVO.getId(),informVO.getMessage());
			resultMessage=informDataService.insert(informPO);
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resultMessage;
	}

}
